((group_by(.[$columnfield])|map({(.[0][$columnfield]):.})|add) // []) 
        as $bycolumns
    | group_by(.[$rowfield])
        |map(.[0][$rowfield] as $rowvalue | {
            rowvalue:$rowvalue,
            group:($bycolumns|with_entries(
                    .value|=(
                        map(select(.[$rowfield]==$rowvalue))
                        |stat_func
                    )
                )
            )
        }) 
    | {data:.|map(.total=(.group|add))
        ,bycolumns: {group:(
            $bycolumns | with_entries(.value|=stat_func)
        )}
    }
    | .bycolumns.total = (.bycolumns.group|reduce .[] as $value(0;.+$value))